package com.zdp.chat.util;


import java.lang.reflect.Field;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class ObjectUtils {

    /**
     * 强制检验对象中的属性是否全部为null
     * @param obj 待验证对象
     * @return true-所有属性都为null / false-部分属性不为null或所有属性不为null
     */
    public static boolean isFieldAllNotEmpty(Object obj) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        AtomicInteger atomic = new AtomicInteger(0);
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            try {
                Object value = field.get(obj);
                if (Objects.nonNull(value)) {
                    atomic.addAndGet(1);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return fields.length == atomic.get();
    }
}
